/**
 * 
 */
package com.eis.persist.dao.util;

import java.io.Serializable;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.jdbc.core.BatchPreparedStatementSetter;

import com.eis.persist.model.user.PersistUser;

/**
 * @author sh-panwei01
 *
 */
public class UserInsertSetter implements BatchPreparedStatementSetter, Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = -9222568105363877016L;
	private List<PersistUser> persistUsers = new ArrayList<PersistUser>();
	private String operatorName;
	
	public UserInsertSetter (List<PersistUser> persistUsers, String operatorName) {
		this.persistUsers = persistUsers;
		this.operatorName = operatorName;
	}
	
	public UserInsertSetter (PersistUser persistUser, String operatorName) {
		this.persistUsers.add(persistUser);
		this.operatorName = operatorName;
	}

	/* (non-Javadoc)
	 * @see org.springframework.jdbc.core.BatchPreparedStatementSetter#setValues(java.sql.PreparedStatement, int)
	 */
	@Override
	public void setValues(PreparedStatement ps, int i) throws SQLException {
		// "insert into tb_user(name,password,sysGroupId,bizGroupId,createDate,createBy) values (?, ?, ?, ?,CURRENT_TIMESTAMP,?);"
		PersistUser persistUser = persistUsers.get(i);
		int offset = 1;
		
		if (persistUser.getName() != null && persistUser.getName().length() > 0) {
			ps.setString(offset, persistUser.getName());
			offset++;
		}
			
		if (persistUser.getPassword() != null && persistUser.getPassword().length() > 0) {
			ps.setString(offset, persistUser.getPassword());
			offset++;
		}
		
		ps.setString(offset, persistUser.getEmail());
		offset++;
		
		if (persistUser.getSysGroup() != null &&  persistUser.getSysGroup().getId() > 0) {
			ps.setInt(offset, persistUser.getSysGroup().getId());
			offset++;
		}
			
		if (persistUser.getBizGroup() != null && persistUser.getBizGroup().getId() > 0) {
			ps.setInt(offset, persistUser.getBizGroup().getId());
			offset++;
		}

		ps.setString(offset, operatorName);
	}

	/* (non-Javadoc)
	 * @see org.springframework.jdbc.core.BatchPreparedStatementSetter#getBatchSize()
	 */
	@Override
	public int getBatchSize() {
		return persistUsers.size();
	}

}
